<template>
  <div class="container">
    <div class="loginBox">
      <a-form-model
        class="loginForm"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item label="账号">
          <a-input v-model="form.username" placeholder="Username">
            <a-icon
              slot="prefix"
              type="user"
              style="color: rgba(0, 0, 0, 0.25)"
          /></a-input>
        </a-form-model-item>

        <a-form-model-item label="密码">
          <a-input
            v-model="form.password"
            type="password"
            @keyup.enter="onSubmit"
            placeholder="Password"
          >
            <a-icon
              slot="prefix"
              type="lock"
              style="color: rgba(0, 0, 0, 0.25)"
            />
          </a-input>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" @click="onSubmit"> 登录 </a-button>
          <a-button style="margin-left: 10px" @click="resetForm">
            取消
          </a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    async onSubmit() {
      const { data: res } = await this.$http.post("/login", this.form);
      if (res.status != 200) {
        return this.$message.error(res.msg);
      } else {
        window.sessionStorage.setItem("token", res.token);
        this.$router.push({ name: "admin" });
      }
    },
    resetForm() {
      (this.form.username = ""), (this.form.password = "");
    },
  },
};
</script>

<style scoped>
.container {
  height: 100%;
  display: flex;
  background-color: #282c34;
  justify-content: center;
  align-items: center;
}
.loginBox {
  width: 450px;
  height: 300px;
  border-radius: 10px;
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
}
.loginForm {
  width: 100%;
  bottom: 10%;
  padding: 0 20px;
}
</style>